<template>
	<view class="orderDetail">
		<!-- 详情信息 -->
		<OrderDetailList :orderDetail="orderDetail" />
		<!-- 财务按钮 -->
		<view class="buttonWrap" v-if="userType === '1' && status === '待审批'">
			<button size="mini" type="primary" @click="handlePass('treasurerPass')">通 过</button>
			<button size="mini" @click="handleReject('treasurerCancel')">拒 绝</button>
		</view>
		<!-- 配送按钮 -->
		<view class="buttonWrap" v-if="userType === '2' && status === '待接单'">
			<button size="mini" type="primary" @click="handleTake">接 单</button>
		</view>
		<view class="buttonWrap" v-else-if="userType === '2' && status === '待取货' && flag">
			<button size="mini" type="warn" @click="handleTakeCancel">放弃接单</button>
			<button size="mini" type="primary" @click="handlePickUp">提货完成</button>
		</view>
		<view class="buttonWrap"
			v-else-if="(userType === '2' && status === '待配送' && flag) || (userType === '2' && status === '待退库' && flag)">
			<button size="mini" type="primary" @click="handleCompleteSend" v-if="status !== '待退库'">配送完成</button>
			<button size="mini" type="warn" @click="handleReject('courierReturn')">退 库</button>
		</view>
		<!-- 仓管、专卖店按钮 -->
		<view class="buttonWrap" v-if="userType === '3' || userType === '4'">
			<button size="mini" type="primary" @click="handleOut" v-if="!statusOut">出 库</button>
			<button size="mini" type="warn" @click="handleIn" v-if="inFlag">确认退库</button>
		</view>
		<!-- 客户按钮 -->
		<view class="buttonWrap" v-if="userType === '5' && (status === '已配送' || status === '待配送')">
			<button size="mini" type="primary" @click="handleClientPass">确认收货</button>
			<button size="mini" @click="handleReject('clientCancel')" v-if="status !== '已配送'">拒 绝</button>
		</view>
		<!-- 派单员按钮 -->
		<view class="buttonWrap" v-if="userType === '7' && status === '待取货'">
			<button size="mini" type="warn" @click="handleWithdraw">撤回订单</button>
		</view>
		<!-- 通过的弹框 -->
		<uni-popup ref="successPopup" type="dialog">
			<uni-popup-dialog type="success" content="操作成功" :duration="2000" @confirm="handleSuccess"
				@close="handleSuccess">
			</uni-popup-dialog>
		</uni-popup>
		<!-- 拒绝的弹框 -->
		<uni-popup ref="rejectPopup" type="dialog">
			<uni-popup-dialog type="warn" mode="input" placeholder="请说明原因" title="说明" :duration="2000"
				@confirm="handleRejectConfirm">
			</uni-popup-dialog>
		</uni-popup>
	</view>
</template>

<script>
	import OrderDetailList from "@/components/orderDetailList.vue"
	export default {
		name: "OrderDetail",
		components: {
			OrderDetailList
		},
		data() {
			return {
				orderId: undefined,
				rejectUrl: "",
				orderDetail: {}
			}
		},
		onLoad(options) {
			this.orderId = options.orderId
		},
		onShow() {
			this.getOrderDetail()
		},
		onPullDownRefresh() {
			this.getOrderDetail()
		},
		computed: {
			status() {
				return this.orderDetail.status
			},
			statusOut() {
				return this.orderDetail.statusOut === 'true' ? true : false
			},
			flag() {
				return this.orderDetail.flag === 'true' ? true : false
			},
			inFlag() {
				return this.orderDetail.inFlag === 'true' ? true : false
			},

		},
		methods: {
			// 获取订单详情
			async getOrderDetail() {
				try {
					const {
						data
					} = await this.request("orderDetail", {
						orderId: this.orderId,
						phone: this.phone,
					})

					this.orderDetail = data.data
					console.log(this.orderDetail)
				} catch (e) {
					console.log(e)
				}
			},
			// 通过
			async handlePass(url) {
				try {
					await this.request(url, {
						phone: this.phone,
						orderId: this.orderId,
					})
					this.$refs.successPopup.open()
				} catch (e) {
					uni.showToast({
						title: "操作失败",
						icon: "error"
					})
				}
			},
			// 拒绝按钮
			handleReject(url) {
				this.rejectUrl = url
				this.$refs.rejectPopup.open()
			},
			// 成功确认弹框
			handleSuccess() {
				this.getOrderDetail()
			},
			/* 拒绝弹框确认按钮事件 */
			async handleRejectConfirm(rejectContent) {
				try {
					await this.request(this.rejectUrl, {
						orderId: this.orderId,
						phone: this.phone,
						content: rejectContent,
					})

					this.$refs.successPopup.open()
				} catch (e) {
					uni.showToast({
						title: "操作失败",
						icon: "error"
					})
				}
			},
			/* 接单按钮 */
			async handleTake() {
				uni.navigateTo({
					url: `/pages/chooseWarehouse/chooseWarehouse?orderDetail=${JSON.stringify(this.orderDetail)}`
				})
			},
			/* 放弃接单 */
			handleTakeCancel() {
				uni.showModal({
					title: "提示",
					content: "确认放弃接单吗？",
					success: async (res) => {
						if (res.confirm) {
							try {
								await this.request("cancelTakeOrder", {
									orderId: this.orderId,
									phone: this.phone,
								})
								this.$refs.successPopup.open()
							} catch (e) {
								uni.showToast({
									title: "操作失败",
									icon: "error"
								})
							}
						}
					}
				})
			},
			// 提货完成按钮
			handlePickUp() {
				uni.showModal({
					title: "提示",
					content: "确认已提货完成？",
					success: async (res) => {
						if (res.confirm) {
							try {
								await this.request("completeGet", {
									orderId: this.orderId,
									phone: this.phone,
								})
								this.$refs.successPopup.open()
							} catch (e) {
								uni.showToast({
									title: "操作失败",
									icon: "error"
								})
							}
						}
					}
				})
			},
			// 配送完成
			async handleCompleteSend() {
				uni.showModal({
					title: "提示",
					content: "确认已配送完成？",
					success: async (res) => {
						if (res.confirm) {
							try {
								await this.request("completeSend", {
									orderId: this.orderId,
									phone: this.phone,
								})
								this.$refs.successPopup.open()
							} catch (e) {
								uni.showToast({
									title: "操作失败",
									icon: "error"
								})
							}
						}
					}
				})
			},
			// 退库
			handleIn() {
				uni.showModal({
					title: "提示",
					content: "确认退库完成吗？",
					success: async (res) => {
						if (res.confirm) {
							try {
								await this.request("warehouseReturn", {
									orderId: this.orderId,
									phone: this.phone,
								})
								this.$refs.successPopup.open()
							} catch (e) {
								uni.showToast({
									title: "操作失败",
									icon: "error"
								})
							}
						}
					}
				})
			},
			// 客户收货
			handleClientPass() {
				uni.showModal({
					title: "提示",
					content: "确认收货吗？",
					success: async (res) => {
						if (res.confirm) {
							try {
								await this.request("clientPass", {
									orderId: this.orderId,
								})
								uni.reLaunch({
									url: '/pages/index/index'
								})
							} catch (e) {
								uni.showToast({
									title: "操作失败",
									icon: "error"
								})
							}
						}
					}
				})
			},
			// 出库按钮
			handleOut() {
				uni.showModal({
					title: "提示",
					content: "确认出库吗？",
					success: async (res) => {
						if (res.confirm) {
							try {
								await this.request("warehouseOut", {
									orderId: this.orderId,
									phone: this.phone,
								})
								this.$refs.successPopup.open()
							} catch (e) {
								uni.showToast({
									title: "操作失败",
									icon: "error"
								})
							}
						}
					}
				})
			},
			// 撤回订单
			handleWithdraw() {
				uni.showModal({
					title: "提示",
					content: "确认撤回订单吗？",
					success: async (res) => {
						if (res.confirm) {
							try {
								await this.request("backoutOrder", {
									orderId: this.orderId,
								})
								this.$refs.successPopup.open()
							} catch (e) {
								uni.showToast({
									title: "操作失败",
									icon: "error"
								})
							}
						}
					}
				})
			},
		}
	}
</script>

<style lang="scss" scoped>
	.orderDetail {
		padding-bottom: 80rpx;
	}
</style>
